package main

import (
	"fmt"
)

func main() {
	fmt.Println(subarraySum([]int{1, 1, 1, 1, 3}, 3))
}

func subarraySum(nums []int, k int) int {
	l := len(nums)
	if l == 0 {
		return 0
	}

	var re int
	var data = make(map[int]int, l)
	data[0] = 1

	presum := 0
	for i := 0; i < l; i++ {
		presum += nums[i]
		if v, ok := data[presum-k]; ok {
			re += v
		}
		data[presum]++
	}
	return re
}
